#include <iostream>
using namespace std;
const int N=1e4+10;
typedef long long LL;
char f[N][N];
int t,p;
int a,b;
char dfs(int x,int y)
{
	f[x][y]='3';
	if(f[x][y])return f[x][y];
	
	if(x==0)return f[x][y]='1';
	if(y==0)return f[x][y]='2';
	
	x=(x+y)%p;
	y=(x+y)%p;
	return f[x][y]=dfs(x,y);	
}
int main()
{
	cin>>t>>p;
	while(t--)
	{
		cin>>a>>b;
		char ret=dfs(a,b);
		if(ret=='1')cout<<1<<endl;
		else if(ret=='2')cout<<2<<endl;
		else cout<<"error"<<endl;
	}
	return 0;
}
